package admin.code.generator.core;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;

import freemarker.log.Logger;
import freemarker.template.Template;

/**
 *
 *@author Jack
 *2016年3月6日下午9:28:35
 */
public class HtmlGenerator {

	private static Logger log = Logger.getLogger(HtmlGenerator.class.getName());

	public static void generate(Map root, Template template, String pageName, String destDir) throws Exception {

		log.info("开始生成" + pageName + ".html");

		//	String realDir = destDir + File.separator + baseMetaData.getPackageName().replace(".", File.separator);
		File fDir = new File(destDir);
		if (!fDir.exists()) {
			fDir.mkdirs();
		}
		String realFile = destDir + File.separator + pageName + ".html";
		//判断,如果文件存在抛出异常,不进行覆盖
		if (destDir.endsWith(File.separator)) {
			realFile = destDir + pageName + ".html";
		}

		//Merge data-model with template 
		FileOutputStream fileOutputStream = new FileOutputStream(new File(realFile));
		Writer out = new OutputStreamWriter(fileOutputStream);
		template.process(root, out);
		fileOutputStream.close();

		log.info(pageName + ".html生成完毕,生成源文件所在目录:" + realFile);
	}
}
